package com.pts.servlet.product;

import com.pts.dao.Dao;
import com.pts.dao.EnterpriseDao;
import com.pts.dao.ProduceDao;
import com.pts.dao.ProductDao;
import com.pts.vo.Enterprise;
import com.pts.vo.Produce;
import com.pts.vo.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.*;

//处理enterprise/list.jsp页面请求，转发到Yes/No页面
//功能是管理员添加生产信息
@WebServlet(value = "/proadd")@MultipartConfig
public class AddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
//        处理文件 Part类
        Part part = request.getPart("fs");

        String fn = UUID.randomUUID().toString();
        String f = part.getSubmittedFileName().toLowerCase();
        //判断文件类型
        String fileend =  f.substring(f.lastIndexOf("."));
        switch(fileend){
            case ".png":
            case ".jpg":
            case ".jpeg":
            case ".gif":
                break;
                default:
                    request.setAttribute("username",request.getParameter("username"));
                    request.getRequestDispatcher("enterprise/tempError.jsp").forward(request, response);
        }
        //重命名
        String filename = fn.concat(f.substring(f.lastIndexOf(".")));
        //获得项目中的一个uri的绝对路径  就在Tomcat的文件夹里面
        //
        String path = request.getServletContext().getRealPath("/upload");
        File ff = new File(path);
        if(!ff.exists()){
            ff.mkdirs();
        }
        //写入文件
        part.write(path+"/"+filename);
        Produce produce = new Produce();
        Product product = new Product();
        //开始准备信息存储到mysql数据库
        //存储文件名就行了
        product.setImg(filename);
        int eid = Integer.parseInt(request.getParameter("eid"));
        produce.setEid(eid);
        produce.setName(request.getParameter("pername"));
        produce.setTel(request.getParameter("tel"));
        produce.setPrice(Float.parseFloat(request.getParameter("price")));
        produce.setPlace(request.getParameter("place"));
        eid %= 100000;

        StringBuilder card = new StringBuilder();
        for (int temp = eid;temp<10000;temp*=10){
            card.append("0");
        }
        card.append(eid);
        String empInfo = card.toString();
        produce.setCard(empInfo+((int) (Math.random() * 90000000) + 10000000));
        Calendar calendar = Calendar.getInstance();
        calendar.set(2022, Calendar.MAY, 6, 0, 0, 0);
        Date begin = calendar.getTime();
        Date end = new Date();
        Date temp = new Date();
        temp.setTime((long) (Math.random() * (end.getTime() - begin.getTime()) + begin.getTime()));
        produce.setDateTime(temp);
        calendar.setTime(temp);
        calendar.add(Calendar.YEAR, 1);
        produce.setEndDate(calendar.getTime());
        product.setName(request.getParameter("name"));
        product.setCard(empInfo+((int) (Math.random() * 900000) + 100000));
        System.out.println("溯源码是:"+product.getCard());
        product.setState(Byte.parseByte(request.getParameter("state")));
        product.setTid(Byte.parseByte(request.getParameter("tid")));
        Dao productDao = new ProductDao();
        Dao produceDao = new ProduceDao();
        produceDao.add(produce);
        int id = produceDao.getId(produce);
        product.setPid(id);
        productDao.add(product);
        List<Enterprise> list = new EnterpriseDao().findAll();
        request.setAttribute("username",request.getParameter("username"));
        request.getRequestDispatcher("admin/enterprise/temp.jsp").forward(request, response);
    }
}
